/*
 * A reusable structure for the udp servers we will use that one
 */

package udpserver;

import java.io.*;
import java.net.*;

/**
 *
 * @author makkalot
 */
public abstract class GeneralUdpServer extends Thread{
    
    private int bufferSize; // in bytes
    protected DatagramSocket ds; // the communication channel
   
  public GeneralUdpServer(int port, int bufferSize) 
   throws SocketException {
    this.bufferSize = bufferSize;
    this.ds = new DatagramSocket(port);
  }
  
  public GeneralUdpServer(int port) throws SocketException {
      //assigning default things  
      this(port, 9999);
  }
  
  //when creating a new thread
  public void run() {
  
    //create a buffer   
    byte[] buffer = new byte[bufferSize];
    
    while (true) {
      DatagramPacket incoming = new DatagramPacket(buffer, buffer.length);
      try {
        ds.receive(incoming);
        this.respond(incoming); // the things you will do with that data
      }
      catch (IOException e) {
        System.err.println(e);
      }      
    } // end while

  }  // end run
  
  //if you want to do something different override that method ...
  public abstract void respond(DatagramPacket request);

}
